/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.listeners;

import wwcontrolserver.datarecorders.AbstractDataRecorder;
import wwcontrolserver.datarecorders.WWControlServerLogger;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import java.util.ArrayList;
import java.util.HashMap;
import org.tmatesoft.sqljet.core.SqlJetException;
import wwcontrolserver.WWControlServer;
import wwcontrolserver.controls.ControlKey;
import wwcontrolserver.controls.ControlManager;
import wwcontrolserver.ifk.AbstractIFK;
/**
 *
 * @author Michael Ferry
 */
public class WWAttachListener implements AttachListener {
    private final WWControlServerLogger WWCSLog;
    private final ArrayList<AbstractDataRecorder> DataRecList;
    private final HashMap IFKMap;
    private final ControlManager CManager;

    public WWAttachListener(WWControlServerLogger WWCSLog, ArrayList<AbstractDataRecorder> DataRecList, HashMap IFKMap, ControlManager CManager) {
        this.WWCSLog = WWCSLog;
        this.DataRecList = DataRecList;
        this.IFKMap = IFKMap;
        this.CManager = CManager;
    }

    @Override
    public void attached(AttachEvent ae) {
        System.out.println("attachment of " + ae);
        WWCSLog.RecordLogEntry("attachment of " + ae);
        
        try {
            Phidget PhidSource = ae.getSource();
            int SerialNumber = PhidSource.getSerialNumber();

            //Insert database record
            for (AbstractDataRecorder DataRec : DataRecList) {
                DataRec.InsertAttachDetachRecord(SerialNumber,true);
            }
            
            AbstractIFK AttachedIFK = (AbstractIFK) IFKMap.get(SerialNumber);
            AttachedIFK.ExecuteDefaultAttach();
            
            CManager.NotifyControlsUsingKey(new ControlKey(SerialNumber));
            
//            if (WWDB != null) {
//                try {
//                    WWDB.InsertAttachDetachRecord(SerialNumber, 1);
//                } catch (SqlJetException sqle) {
//                    System.out.println(sqle);
//                    WWCS.RecordLogEntry(sqle.toString());
//                }
//            }
            //Prints the serial number of attached Phidgets
            //System.out.println(SerialNumber);
            
//            if (SerialNumber == WWControlServer.ifk_pHORP_serial) {
//                InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//                ifk.setRatiometric(WWControlServer.DefaultRatiometric_pHORP);
//                //System.out.println("Set Ratiometric for pH/ORP interface");
//            } else if (SerialNumber == WWControlServer.ifk_HeaterSensor_serial) {
//                InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//                ifk.setRatiometric(WWControlServer.DefaultRatiometric_HeaterSensor);
//                //System.out.println("Set Ratiometric for Heater Sensor interface");
//                for (int i = 0; i < WWControlServer.HeaterSensorMap.length; i++) {
//                    ifk.setSensorChangeTrigger(WWControlServer.HeaterSensorMap[i], WWControlServer.LOW_TRIGGER);
//                }
//            } else if (SerialNumber == WWControlServer.ifk_HeaterRelay_serial) {
//            } else if (SerialNumber == WWControlServer.ifk_MotorRelay) {
//                WWCS.SetDCMotorState(WWCS.DCMotorState);
//            } else if (SerialNumber == WWControlServer.ifk_P1_2_serial) {
//                InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//                ifk.setRatiometric(WWControlServer.DefaultRatiometric_P1_2);
//                // System.out.println("Set Ratiometric for P1_2 interface");
//            } else if (SerialNumber == WWControlServer.ifk_P3_serial) {
//                InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//                ifk.setRatiometric(WWControlServer.DefaultRatiometric_P3);
//                //System.out.println("Set Ratiometric for P3_4 interface");
//            } else if (SerialNumber == WWControlServer.ifk_P4_serial) {
//                InterfaceKitPhidget ifk = (InterfaceKitPhidget) PhidSource;
//                ifk.setRatiometric(WWControlServer.DefaultRatiometric_P4);
//                //System.out.println("Set Ratiometric for P3_4 interface");
//            }
        } catch (PhidgetException phde) {
            WWCSLog.RecordExceptionLogEntry(phde);
        }
    }
}
